//
//  IGMePubTopicVC.m
//  jingdongfang
//
//  Created by BOE on 2017/8/22.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGMePubTopicVC.h"
#import "IGCommunitySearchVC.h"
#import "IGTopicDetailVC.h"
#import "IGMePubTopicApi.h"
#import "IGNetReachable.h"
#import "IGEmptyView.h"

@interface IGMePubTopicVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) IGMePubTopicApi *mePubTopicApi;
@property (nonatomic, copy) NSString *lastTimeDate;
@property (nonatomic, assign) NSInteger page;
    
@end

@implementation IGMePubTopicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.pubTitle?:NSLocalizedString(@"My Topics", nil);
    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
    self.lastTimeDate = @"0";
    self.page = 1;
    self.mePubTopicApi = [[IGMePubTopicApi alloc] initWithLastTimeDate:self.lastTimeDate];
    self.mePubTopicApi.page = self.page;
    self.requestApi = self.mePubTopicApi;
    
    self.heightForFirstSectionCell = 0.1f;
    [self ig_setupRightButtonItemWithTitle:nil image:[UIImage imageNamed:@"app_search_icon"] highLightedImage:[UIImage imageNamed:@"app_search_icon"]];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView ig_setHeaderWithRefreshingBlock:^{
        weakSelf.lastTimeDate = @"0";
        weakSelf.page = 1;
        weakSelf.mePubTopicApi.lastTimeDate = weakSelf.lastTimeDate;
        weakSelf.mePubTopicApi.page = weakSelf.page;
        
        [weakSelf requestData];
    }];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self requestData];
}

- (void)requestData {
    __weak typeof(self) weakSelf = self;
    self.mePubTopicApi.pubUid = _pubUid;
    [self.mePubTopicApi startWithDecodeClass:[IGMePubTopicResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.tableView ig_endHeaderRefreshing];
        [weakSelf.tableView ig_endFooterRefreshing];
        if (responseObject) {
            IGMePubTopicResp *resp = responseObject;
            IGMePubTopicModel *model = resp.data;
            
            NSMutableArray *tmpTopicArr = model.topics;

            if (tmpTopicArr && tmpTopicArr.count > 0) {
                [weakSelf setupFooter];
            } else {
                [weakSelf.tableView ig_endFooterRefreshingWithNoMoreData];
            }
            
            if (weakSelf.page == 1) {
                weakSelf.dataSource = tmpTopicArr;
            } else {
                [weakSelf.dataSource addObjectsFromArray:tmpTopicArr];
            }
            
            weakSelf.lastTimeDate = model.lastTopicTime;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    }];
}
    
- (void)setupFooter {
    __weak typeof(self) weakSelf = self;
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        weakSelf.page++;
        weakSelf.mePubTopicApi.lastTimeDate = weakSelf.lastTimeDate;
        weakSelf.mePubTopicApi.page = weakSelf.page;
        
        [weakSelf requestData];
    }];
} 

#pragma mark -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isShowKeyBoard) {
        [self.view endEditing:YES];
        return;
    }
    
    IGTopicModel *model = self.dataSource[indexPath.row];
    IGTopicDetailVC *detailVC = [[IGTopicDetailVC alloc] initWithTopicId:model.tid];
    [UIViewController ig_pushViewController:detailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

//搜索
- (void)ig_rightButtonPressed:(id)sender {
    [UIViewController ig_pushViewControllerClass:[IGCommunitySearchVC class] animated:YES];
}

#pragma mark -Empty
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    IGEmptyView *emptyView = [[IGEmptyView alloc] init];
    emptyView.requestApi = self.requestApi;
    emptyView.title = NSLocalizedString(@"No Topic Posted by You", nil);
    emptyView.image = [UIImage imageNamed:@"notopic_placehold"];
    return emptyView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
